﻿
#include "FileOperation.h"

FileOperation::FileOperation(QObject *parent)
    : QObject{parent}
{

}

/**
 * @brief FileOperation::addTopic 添加主题
 * @param topicId 主题Id
 * @param topicName 主题名称
 */
void FileOperation::addTopic(int &topicId, QString &topicName)
{
    MsgData topicInfo;
    if(m_TopicId_InfoMap.isEmpty()){
        topicInfo.msgId = 1;
        topicInfo.msgName = "主题名称_1";
    }else{
        topicInfo.msgId = m_TopicId_InfoMap.last().msgId.toInt()+1;
        topicInfo.msgName = "主题名称_"+topicInfo.msgId.toString();
    }
    topicInfo.msgDataLength=0;
    //若集合内主题名称重复，则重新计算赋值
    while(m_TopicName_IdMap.contains(topicInfo.msgName)){
        topicInfo.msgId = topicInfo.msgId.toInt()+1;
        topicInfo.msgName = "主题名称_"+topicInfo.msgId.toString();
    }
    topicId=topicInfo.msgId.toInt();
    topicName=topicInfo.msgName;

    //更新集合信息
    m_TopicId_InfoMap.insert(topicInfo.msgId.toInt(),topicInfo);
    m_TopicName_IdMap.insert(topicInfo.msgName,topicInfo.msgId.toInt());
}

/**
 * @brief FileOperation::deleteTopic 删除主题
 * @param topicId 主题Id
 */
void FileOperation::deleteTopic(const QVariant &topicId)
{
    if(m_TopicId_InfoMap.contains(topicId.toInt())){
        m_TopicName_IdMap.remove(m_TopicId_InfoMap.value(topicId.toInt()).msgName);
        m_TopicId_InfoMap.remove(topicId.toInt());
    }
}

/**
 * @brief FileOperation::addStruct 添加结构体
 * @param structId 结构体Id
 * @param StructName 结构体名称
 */
void FileOperation::addStruct(int &structId, QString &StructName)
{
    SignalData structInfo;
    if(m_StructId_InfoMap.isEmpty()){
        structInfo.signalGuid = 1;
        structInfo.signalName = "结构体名称_1";
    }else{
        structInfo.signalGuid = m_StructId_InfoMap.last().signalGuid.toInt()+1;
        structInfo.signalName = "结构体名称_"+structInfo.signalGuid.toString();
    }
    structInfo.isSignalTheme = true;
    structInfo.offsetByte=0;
    structInfo.offsetBit=0;
    structInfo.bitLength=0;
    structInfo.dataType=0;
    structInfo.LSB=0;
    structInfo.unit=UnitData();
    structInfo.childSignalVec.append("");
    //若集合内主题名称重复，则重新计算赋值
    while(m_TopicName_IdMap.contains(structInfo.signalName)){
        structInfo.signalGuid = structInfo.signalGuid.toInt()+1;
        structInfo.signalName = "结构体名称_"+structInfo.signalGuid.toString();
    }
    structId=structInfo.signalGuid.toInt();
    StructName=structInfo.signalName;

    //更新集合信息
    m_StructId_InfoMap.insert(structInfo.signalGuid.toInt(),structInfo);
    m_StructName_IdMap.insert(structInfo.signalName,structInfo.signalGuid.toInt());
}

/**
 * @brief FileOperation::deleteStruct 删除结构体
 * @param structId 结构体Id
 */
void FileOperation::deleteStruct(const QVariant &structId)
{
    if(m_StructId_InfoMap.contains(structId.toInt())){
        m_StructName_IdMap.remove(m_StructId_InfoMap.value(structId.toInt()).signalName);
        m_StructId_InfoMap.remove(structId.toInt());
    }
}

/**
 * @brief FileOperation::topicCellValueChanged 主题单元格值改变
 * @param rowId 行Id
 * @param columnIndex 列索引
 * @param value 改变后的值
 * @return 文件操作结果码
 */
ResultCode FileOperation::topicCellValueChanged(const QVariant &rowId, const int columnIndex, const QVariant &value)
{
    if(columnIndex==0){ //主题Id
        if(m_TopicId_InfoMap.contains(rowId.toInt())){ //原rowId存在
            if(m_TopicId_InfoMap.contains(value.toInt())){ //修改后的主题Id存在重复
                return Topic_Id_Exists;
            }else{ //修改后的主题Id不存在重复
                //取出原主题Id（rowId）对应的主题信息
                MsgData topicInfo = m_TopicId_InfoMap[rowId.toInt()];
                topicInfo.msgId = value.toInt();
                //移除原主题Id（rowId）相关集合的信息
                m_TopicName_IdMap.remove(m_TopicId_InfoMap[rowId.toInt()].msgName);
                m_TopicId_InfoMap.remove(rowId.toInt());
                //插入新的主题Id，更新集合信息
                m_TopicId_InfoMap.insert(value.toInt(),topicInfo);
                m_TopicName_IdMap.insert(topicInfo.msgName,value.toInt());
                return Success;
            }
        }
    }else if(columnIndex==1){ //主题名称
        if(m_TopicId_InfoMap.contains(rowId.toInt())){ //原rowId存在
            if(m_TopicName_IdMap.contains(value.toString())){ //修改后的主题名称存在重复
                return Topic_Name_Exists;
            }else{ //修改后的主题名称不存在重复
                //更新主题名称，更新信息集合
                m_TopicName_IdMap.remove(m_TopicId_InfoMap[rowId.toInt()].msgName);
                m_TopicId_InfoMap[rowId.toInt()].msgName = value.toString();
                m_TopicName_IdMap.insert(m_TopicId_InfoMap[rowId.toInt()].msgName,m_TopicId_InfoMap[rowId.toInt()].msgId.toInt());
                return Success;
            }
        }
    }else{ //主题长度
        m_TopicId_InfoMap[rowId.toInt()].msgDataLength = value.toInt();
        return Success;
    }
    //原rowId不存在
    return Topic_Non_Exists;
}

/**
 * @brief FileOperation::signalCellValueChanged 信号单元格值改变
 * @param rowId 行Id
 * @param columnIndex 列索引
 * @param value 改变后的值
 * @return 文件操作结果码
 */
ResultCode FileOperation::signalCellValueChanged(const QVariant &rowId, const int columnIndex, const QVariant &value)
{
    if(m_SignalId_InfoMap.contains(rowId.toInt())){ //如果rowId存在
        switch(columnIndex){
        case 0:  //信号Id修改
            if(m_SignalId_InfoMap.contains(value.toInt())){ //修改后的信号Id存在重复
                return Signal_Id_Exists;
            }else{ //修改后的信号Id不存在重复
                //取出原信号Id（rowId）对应的信号信息
                SignalData signalInfo = m_SignalId_InfoMap[rowId.toInt()];
                signalInfo.signalGuid = value.toInt();
                //移除原信号Id（rowId）相关集合的信息
                m_SignalName_IdMap.remove(m_SignalId_InfoMap[rowId.toInt()].signalName);
                m_SignalId_InfoMap.remove(rowId.toInt());
                //插入新的主题Id，更新集合信息
                m_SignalId_InfoMap.insert(value.toInt(),signalInfo);
                m_SignalName_IdMap.insert(signalInfo.signalName,value.toInt());
            }
            break;
        case 1:   //信号名称修改
            if(m_SignalName_IdMap.contains(value.toString())){ //修改后的信号名称存在重复
                return Signal_Name_Exists;
            }else{ //修改后的主题名称不存在重复
                //更新主题名称，更新信息集合
                m_SignalName_IdMap.remove(m_SignalId_InfoMap[rowId.toInt()].signalName);
                m_SignalId_InfoMap[rowId.toInt()].signalName = value.toString();
                m_SignalName_IdMap.insert(m_SignalId_InfoMap[rowId.toInt()].signalName,m_SignalId_InfoMap[rowId.toInt()].signalGuid.toInt());
            }
            break;
        case 2: //数据类型修改
            m_SignalId_InfoMap[rowId.toInt()].dataType=value.toInt();
            break;
        case 3://偏移字
            m_SignalId_InfoMap[rowId.toInt()].offsetByte=value.toInt();
            break;
        case 4://偏移位
            m_SignalId_InfoMap[rowId.toInt()].offsetBit=value.toInt();
            break;
        case 5://长度修改
            m_SignalId_InfoMap[rowId.toInt()].bitLength=value.toInt();
            break;
        case 6://精度修改
            m_SignalId_InfoMap[rowId.toInt()].LSB=value.toInt();
            break;
        case 7://单位修改
//            m_SignalId_InfoMap[rowId.toInt()].unit=value.toInt();
            break;
        }
        return Success;
    }else{ //如果rowId不存在
        return Topic_Non_Exists;
    }
}

/**
 * @brief FileOperation::addSignal 添加信号
 * @param topicId 所属主题Id
 * @return 该信号的信息
 */
QVector<QVariant> FileOperation::addSignal(const QVariant &topicId)
{
    QVector<QVariant> signalVec;
    SignalData signalInfo;
    if(m_SignalId_InfoMap.isEmpty()){
        signalInfo.signalGuid = 1;
        signalInfo.signalName = "信号名称_1";
    }else{
        signalInfo.signalGuid = m_SignalId_InfoMap.last().signalGuid.toInt()+1;
        signalInfo.signalName = "信号名称_"+signalInfo.signalGuid.toString();
    }
    signalInfo.isSignalTheme = true;
    signalInfo.offsetByte=0;
    signalInfo.offsetBit=0;
    signalInfo.bitLength=0;
    signalInfo.dataType=0;
    signalInfo.LSB=0;
    signalInfo.unit=UnitData();
    signalInfo.childSignalVec.append("");
    //若集合内主题名称重复，则重新计算赋值
    while(m_SignalName_IdMap.contains(signalInfo.signalName)){
        signalInfo.signalGuid = signalInfo.signalGuid.toInt()+1;
        signalInfo.signalName = "信号名称_"+signalInfo.signalGuid.toString();
    }
    //更新集合信息和所属主题的信号列表
    m_TopicId_InfoMap[topicId.toInt()].childrenSignalVec.append(signalInfo.signalGuid);
    m_SignalId_InfoMap.insert(signalInfo.signalGuid.toInt(),signalInfo);
    m_SignalName_IdMap.insert(signalInfo.signalName,signalInfo.signalGuid.toInt());
    //设置返回值
    signalVec.append(signalInfo.signalGuid);
    signalVec.append(signalInfo.signalName);
    signalVec.append(signalInfo.dataType);
    signalVec.append(signalInfo.offsetByte);
    signalVec.append(signalInfo.offsetBit);
    signalVec.append(signalInfo.bitLength);
    signalVec.append(signalInfo.LSB);
//    signalVec.append(signalInfo.unit);
    return signalVec;
}

/**
 * @brief FileOperation::deleteSignal 删除信号
 * @param topicId 主题Id
 * @param signalId 信号Id
 */
void FileOperation::deleteSignal(const int &topicId, const QVariant &signalId)
{
    if(m_SignalId_InfoMap.contains(signalId.toInt())){
        m_TopicId_InfoMap[topicId].childrenSignalVec.removeOne(signalId.toString());
        m_SignalName_IdMap.remove(m_SignalId_InfoMap.value(signalId.toInt()).signalName);
        m_SignalId_InfoMap.remove(signalId.toInt());
    }
}

/**
 * @brief getTopicRowData 获取主题行数据
 * @param rowId 行Id
 * @return 该行Id的整行数据
 */
QVector<QVariant> FileOperation::getTopicRowData(const QVariant &rowId)
{
    QVector<QVariant> dataVec;
    if(!m_TopicId_InfoMap.contains(rowId.toInt())){
        dataVec.append(m_TopicId_InfoMap[rowId.toInt()].msgId);
        dataVec.append(m_TopicId_InfoMap[rowId.toInt()].msgName);
        dataVec.append(m_TopicId_InfoMap[rowId.toInt()].msgDataLength);
    }
    return dataVec;
}

/**
 * @brief getSignalRowData 获取信号行数据
 * @param rowId 行Id
 * @return 该行Id的整行数据
 */
QVector<QVariant> FileOperation::getSignalRowData(const QVariant &rowId)
{
    QVector<QVariant> dataVec;
    if(!m_SignalId_InfoMap.contains(rowId.toInt())){
        dataVec.append(m_SignalId_InfoMap[rowId.toInt()].signalGuid);
        dataVec.append(m_SignalId_InfoMap[rowId.toInt()].signalName);
        dataVec.append(m_SignalId_InfoMap[rowId.toInt()].dataType);
        dataVec.append(m_SignalId_InfoMap[rowId.toInt()].offsetByte);
        dataVec.append(m_SignalId_InfoMap[rowId.toInt()].offsetBit);
        dataVec.append(m_SignalId_InfoMap[rowId.toInt()].bitLength);
        dataVec.append(m_SignalId_InfoMap[rowId.toInt()].LSB);
//        dataVec.append(m_SignalId_InfoMap[rowId.toInt()].unit);
    }
    return dataVec;
}

/**
 * @brief LoadSignalInfo 加载信号信息
 * @param type 信号所属父级的类型
 * @param selectId 父级所选的Id（主题Id或结构体Id）
 */
void FileOperation::LoadSignalInfo(ParentType parentType, const QVariant &selectId)
{

}

